package com.food.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.food.user.dto.CommentInfoDto;
import com.food.user.entity.*;
import com.food.user.form.CommentOrderForm;
import com.food.user.form.UserQueryCommentForm;
import com.food.user.mapper.CommentAttachmentMapper;
import com.food.user.mapper.CommentInfoMapper;
import com.food.user.mapper.StoreInfoMapper;
import com.food.user.mapper.UserAccountMapper;
import com.food.user.service.CommentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.user.service.OrderService;
import com.food.user.service.StoreInfoService;
import com.food.user.util.DateUtils;
import com.food.user.util.JwtUtil;
import com.food.user.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 评价信息表 服务实现类
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月17日
 */
@Service
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoMapper, CommentInfo> implements CommentInfoService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserAccountMapper userAccountMapper;
    @Autowired
    private CommentInfoMapper commentInfoMapper;
    @Autowired
    private CommentAttachmentMapper commentAttachmentMapper;
    @Autowired
    private StoreInfoService storeInfoService;

    @Override
    @Transactional
    public CommentInfo commentOrder(CommentOrderForm form) throws Exception {
        //获取用户id
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer userId = JwtUtil.getUserId(accessToken);

        //拿到用户对象 可取到评论人昵称
        UserAccount userAccount = userAccountMapper.selectById(Long.valueOf(userId));
        //new一个commentInfo对象 赋值
        CommentInfo commentInfo = BeanUtil.toBean(form, CommentInfo.class);

        //根据订单编号和用户id查找订单
        Order order = orderService.getOne(new QueryWrapper<Order>().eq("order_num", form.getCommentInfoOrderNum())
                .eq("order_user_id", userId));
        if (order==null){
            throw new Exception("评论失败");
        }
        if (order.getOrderUserId()!=userId.longValue()){
            throw new Exception("违规操作");
        }
        if (order.getOrderEvaluate()==1){
            throw new Exception("不能重复评价");
        }
        //修改订单评价状态
        order.setOrderEvaluate(1);
        orderService.updateById(order);

        commentInfo.setCommentInfoStoreId(order.getOrderStoreId());
        //如果用户有无上传评论图片
        if(form.getPicture()==null){
            commentInfo.setCommentInfoPictureFlag(0);
        }
        commentInfo.setCommentInfoPictureFlag(1);
        commentInfo.setCommentInfoUserName(userAccount.getUserNickName());
        commentInfo.setCommentInfoUserId(Long.valueOf(userId));
        commentInfo.setCommentInfoDate(DateUtils.parseString(LocalDateTime.now(),"yyyy-MM-hh HH:mm:ss"));
        commentInfo.setCommentInfoState(0);
        commentInfoMapper.insert(commentInfo);
        //更新附件表
        CommentAttachment commentAttachment = new CommentAttachment();
        commentAttachment.setCommentAttachmentCommentInfoId(commentInfo.getCommentInfoId());
        commentAttachment.setCommentAttachmentFilePath(form.getPicture());
        commentAttachment.setCommentAttachmentCreateDate(LocalDateTime.now());
        commentAttachmentMapper.insert(commentAttachment);
        return commentInfo;
    }

    @Override
    public List<CommentInfo>  queryCommentByOrderNum(Long storeId) throws Exception {

        //判断商铺是否存在
        StoreInfo store = storeInfoService.getById(storeId);
        if (store==null){
            throw new Exception("商铺不存在");
        }
        List<CommentInfo> commentInfos= list(new QueryWrapper<CommentInfo>().eq("comment_info_store_id", storeId).orderByAsc("comment_info_date"));

        return commentInfos;
    }


}
